/*****************************************************************************************
 * FILE NAME: test_main_chaodian_can_L                   COPYRIGHT (c) TX 2025
 * REVISION:  1.0.0                                     All Rights Reserved
 * DESCRIPTION: 超电CAN协议模块单元测试主程序
 * =======================================================================================
 * UPDATE HISTORY：
 * REV      AUTHOR      DATE
 * v1.0.0   fangangchuan  2025.10.15
 * DESCRIPTION OF CHANGE: 创建超电CAN协议模块的单元测试主程序
 ****************************************************************************************/

//----------------------------------------------------------------------------------------
// @Project Includes
#include "test_chaodian_can_L.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module

//----------------------------------------------------------------------------------------
// @Local Variables

//----------------------------------------------------------------------------------------
// 主测试运行器
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void run_all_tests(void)
 * @Description: 运行所有测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void run_all_tests(void)
{
    printf("\n");
    printf("========================================\n");
    printf("    Chaodian CAN Protocol Unit Tests Start\n");
    printf("========================================\n");
    printf("Test Time: %s %s\n", __DATE__, __TIME__);
    printf("Compiler: %s\n", __VERSION__);
    printf("========================================\n\n");
    
    // 初始化测试框架
    test_framework_init();
    
    // 运行所有测试套件
    test_suite_basic_functions();
    test_suite_data_transform();
    test_suite_register_operations();
    test_suite_message_processing();
    test_suite_upgrade_functions();
    test_suite_integration();
    // test_suite_upgrade_production();
    
    // 打印最终统计信息
    printf("\n");
    printf("========================================\n");
    printf("    Chaodian CAN Protocol Unit Tests Completed\n");
    printf("========================================\n");
    test_framework_print_stats();
    printf("========================================\n");
    
    // 清理测试框架
    test_framework_cleanup();
}

/*
 * @Functions:   void run_specific_test_suite(const char* suite_name)
 * @Description: 运行指定的测试套件
 * @Parameters:  suite_name - 测试套件名称
 * @Return:      NONE
 */
void run_specific_test_suite(const char* suite_name)
{
    if (suite_name == NULL)
    {
        printf("错误: 测试套件名称为空\n");
        return;
    }
    
    printf("\n");
    printf("========================================\n");
    printf("    Running Test Suite: %s\n", suite_name);
    printf("========================================\n\n");
    
    // 初始化测试框架
    test_framework_init();
    
    // 根据套件名称运行对应测试
    if (strcmp(suite_name, "basic") == 0)
    {
        test_suite_basic_functions();
    }
    else if (strcmp(suite_name, "transform") == 0)
    {
        test_suite_data_transform();
    }
    else if (strcmp(suite_name, "register") == 0)
    {
        test_suite_register_operations();
    }
    else if (strcmp(suite_name, "message") == 0)
    {
        test_suite_message_processing();
    }
    else if (strcmp(suite_name, "upgrade") == 0)
    {
        test_suite_upgrade_functions();
    }
    else if (strcmp(suite_name, "integration") == 0)
    {
        test_suite_integration();
    }
    else
    {
        printf("Error: Unknown test suite name: %s\n", suite_name);
        printf("Available test suites:\n");
        printf("  - basic: Basic utility functions test\n");
        printf("  - transform: Data transform functions test\n");
        printf("  - register: Register operation functions test\n");
        printf("  - message: Message processing functions test\n");
        printf("  - upgrade: Upgrade related functions test\n");
        printf("  - integration: Integration test\n");
        return;
    }
    
    // 打印统计信息
    printf("\n");
    printf("========================================\n");
    printf("    Test Suite Completed: %s\n", suite_name);
    printf("========================================\n");
    test_framework_print_stats();
    printf("========================================\n");
    
    // 清理测试框架
    test_framework_cleanup();
}

/*
 * @Functions:   void run_performance_tests(void)
 * @Description: 运行性能测试
 * @Parameters:  NONE
 * @Return:      NONE
 */
void run_performance_tests(void)
{
    printf("\n");
    printf("========================================\n");
    printf("    Chaodian CAN Protocol Performance Tests\n");
    printf("========================================\n\n");
    
    uint32_t start_time, end_time, duration;
    uint32_t iterations = 10000;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 测试CAN ID构建性能
    printf("Testing CAN ID build performance (%d iterations)...\n", iterations);
    start_time = mock_drv_systick_getTick();
    
    for (uint32_t i = 0; i < iterations; i++)
    {
        cd_can_build_id(CD_FUNC_READ_REG, i & 0xFFFF, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
    }
    
    end_time = mock_drv_systick_getTick();
    duration = end_time - start_time;
    printf("CAN ID build performance: %d ms (%d iterations), Average: %.2f us/iteration\n", 
           duration, iterations, (float)duration * 1000.0f / iterations);
    
    // 测试CAN ID解析性能
    printf("Testing CAN ID parse performance (%d iterations)...\n", iterations);
    start_time = mock_drv_systick_getTick();
    
    uint8_t func_code, dst_addr, src_addr;
    uint16_t reg_addr;
    
    for (uint32_t i = 0; i < iterations; i++)
    {
        uint32_t can_id = cd_can_build_id(CD_FUNC_READ_REG, i & 0xFFFF, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
        cd_can_parse_id(can_id, &func_code, &reg_addr, &dst_addr, &src_addr);
    }
    
    end_time = mock_drv_systick_getTick();
    duration = end_time - start_time;
    printf("CAN ID parse performance: %d ms (%d iterations), Average: %.2f us/iteration\n", 
           duration, iterations, (float)duration * 1000.0f / iterations);
    
    // 测试寄存器查找性能
    printf("Testing register lookup performance (%d iterations)...\n", iterations);
    start_time = mock_drv_systick_getTick();
    
    for (uint32_t i = 0; i < iterations; i++)
    {
        cd_can_find_reg_map(i % 100);  // 查找0-99范围内的寄存器
    }
    
    end_time = mock_drv_systick_getTick();
    duration = end_time - start_time;
    printf("Register lookup performance: %d ms (%d iterations), Average: %.2f us/iteration\n", 
           duration, iterations, (float)duration * 1000.0f / iterations);
    
    // 测试消息处理性能
    printf("Testing message processing performance (%d iterations)...\n", iterations);
    start_time = mock_drv_systick_getTick();
    
    uint8_t data[8] = {CD_FUNC_READ_REG, 0x00, 0x01, 0x00, CD_DATA_FILL, CD_DATA_FILL, CD_DATA_FILL, CD_DATA_FILL};
    
    for (uint32_t i = 0; i < iterations; i++)
    {
        uint32_t can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0000, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
        cd_can_process_message(can_id, data, 8);
    }
    
    end_time = mock_drv_systick_getTick();
    duration = end_time - start_time;
    printf("Message processing performance: %d ms (%d iterations), Average: %.2f us/iteration\n", 
           duration, iterations, (float)duration * 1000.0f / iterations);
    
    printf("\n========================================\n");
    printf("    Performance Tests Completed\n");
    printf("========================================\n");
}

/*
 * @Functions:   void run_stress_tests(void)
 * @Description: 运行压力测试
 * @Parameters:  NONE
 * @Return:      NONE
 */
void run_stress_tests(void)
{
    printf("\n");
    printf("========================================\n");
    printf("    Chaodian CAN Protocol Stress Tests\n");
    printf("========================================\n\n");
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 测试大量消息处理
    printf("Testing large message processing...\n");
    uint32_t message_count = 1000;
    uint8_t data[8];
    
    for (uint32_t i = 0; i < message_count; i++)
    {
        // 交替发送读和写请求
        uint32_t can_id;
        if (i % 2 == 0)
        {
            can_id = cd_can_build_id(CD_FUNC_READ_REG, i % 100, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
            data[0] = CD_FUNC_READ_REG;
            data[1] = 0x00; data[2] = 0x01;
            data[3] = 0x00;
            memset(&data[4], CD_DATA_FILL, 4);
        }
        else
        {
            can_id = cd_can_build_id(CD_FUNC_WRITE_REG, i % 100, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
            data[0] = CD_FUNC_WRITE_REG;
            data[1] = 0x00; data[2] = 0x01;
            data[3] = 0x00;
            data[4] = (i >> 8) & 0xFF; data[5] = i & 0xFF;
            data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
        }
        cd_can_process_message(can_id, data, 8);
        
        if (i % 100 == 0)
        {
            printf("Processed %d messages...\n", i);
        }
    }
    
    printf("Stress test completed, processed %d messages\n", message_count);
    
    // 检查统计信息
    uint32_t rx_count, tx_count, error_count, timeout_count;
    cd_can_get_stats(&rx_count, &tx_count, &error_count, &timeout_count);
    
    printf("Statistics:\n");
    printf("  Received messages: %d\n", rx_count);
    printf("  Sent messages: %d\n", tx_count);
    printf("  Error count: %d\n", error_count);
    printf("  Timeout count: %d\n", timeout_count);
    
    printf("\n========================================\n");
    printf("    Stress Tests Completed\n");
    printf("========================================\n");
}

/*
 * @Functions:   void print_test_help(void)
 * @Description: 打印测试帮助信息
 * @Parameters:  NONE
 * @Return:      NONE
 */
void print_test_help(void)
{
    printf("\n");
    printf("========================================\n");
    printf("    Chaodian CAN Protocol Unit Test Help\n");
    printf("========================================\n");
    printf("Usage:\n");
    printf("  run_all_tests()              - Run all tests\n");
    printf("  run_specific_test_suite(name) - Run specific test suite\n");
    printf("  run_performance_tests()      - Run performance tests\n");
    printf("  run_stress_tests()          - Run stress tests\n");
    printf("  print_test_help()           - Show this help information\n");
    printf("\n");
    printf("Available test suites:\n");
    printf("  - basic: Basic utility functions test\n");
    printf("  - transform: Data transform functions test\n");
    printf("  - register: Register operation functions test\n");
    printf("  - message: Message processing functions test\n");
    printf("  - upgrade: Upgrade related functions test\n");
    printf("  - integration: Integration test\n");
    printf("\n");
    printf("Examples:\n");
    printf("  run_specific_test_suite(\"basic\");\n");
    printf("  run_specific_test_suite(\"message\");\n");
    printf("========================================\n");
}

//----------------------------------------------------------------------------------------
// 主程序入口
//----------------------------------------------------------------------------------------

/*
 * @Functions:   int main(void)
 * @Description: 主程序入口
 * @Parameters:  NONE
 * @Return:      程序退出码
 */
int main(void)
{
    printf("Chaodian CAN Protocol Unit Test Program Started\n");
    
    // 显示帮助信息
    print_test_help();
    
    // 运行所有测试
    run_all_tests();
    
    // 运行性能测试
    run_performance_tests();
    
    // 运行压力测试
    run_stress_tests();
    
    printf("\nAll tests completed, program exiting\n");
    return 0;
}

//----------------------------------------------------------------------------------------
// 测试用例示例
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void demo_test_usage(void)
 * @Description: 演示测试用例使用方法
 * @Parameters:  NONE
 * @Return:      NONE
 */
void demo_test_usage(void)
{
    printf("\n");
    printf("========================================\n");
    printf("    Demo Test Usage Example\n");
    printf("========================================\n");
    
    // 示例1: 测试单个函数
    printf("Example 1: Test CAN ID Build Function\n");
    uint32_t can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0001, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
    printf("构建的CAN ID: 0x%08X\n", can_id);
    
    // 示例2: 测试数据转换
    printf("\nExample 2: Test Speed Data Transform\n");
    uint16_t speed_ms = 50;  // 50 m/s
    uint8_t data[4];
    uint8_t result = cd_transform_speed(CD_TRANSFORM_READ, &speed_ms, data, sizeof(data));
    printf("转换结果: %d字节, 数据: %02X %02X\n", result, data[0], data[1]);
    
    // 示例3: 测试寄存器操作
    printf("\nExample 3: Test Register Read\n");
    cd_can_init(CD_DEVICE_ADDR_BMS);
    uint8_t reg_data[8];
    const stc_cd_reg_map_t *reg_map = cd_can_find_reg_map(0);
    if (reg_map != NULL)
    {
        result = cd_can_read_data_from_ptr(reg_map, reg_data, sizeof(reg_data));
        printf("Register 0 Read Result: %d bytes\n", result);
    }
    else
    {
        printf("Register 0 not found\n");
    }
    
    // 示例4: 测试消息处理
    printf("\nExample 4: Test Message Processing\n");
    uint8_t msg_data[8] = {CD_FUNC_READ_REG, 0x00, 0x01, 0x00, CD_DATA_FILL, CD_DATA_FILL, CD_DATA_FILL, CD_DATA_FILL};
    uint32_t msg_can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0000, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
    cd_can_process_message(msg_can_id, msg_data, 8);
    printf("Message Processing Completed\n");
    
    printf("\n========================================\n");
    printf("    Demo Test Usage Example Completed\n");
    printf("========================================\n");
}

// =======================================================================================
// End of file.
// =======================================================================================
